import java.util.*;

/**
 * 5. 最长回文子串
 * <p>
 * 给定一个字符串 s，找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
 * <p>
 * 示例 1：
 * <p>
 * 输入: "babad"
 * 输出: "bab"
 * 注意: "aba" 也是一个有效答案。
 * <p>
 * 示例 2：
 * <p>
 * 输入: "cbbd"
 * 输出: "bb"
 */
public class Solution {
    public static void main(String[] args) {
        longestPalindrome("civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth");
    }


    public static String longestPalindrome(String s) {
        //如果字符串为空 直接返回空串
        if (s == null || s.length() < 1) return "";
        //定义开始 末尾指针
        int start = 0, end = 0;


        for (int i = 0; i < s.length(); i++) {
            int len1 = expandAroundCenter(s, i, i);
            int len2 = expandAroundCenter(s, i, i + 1);
            int len = Math.max(len1, len2);
            if (len > end - start) {
                start = i - (len - 1) / 2;
                end = i + len / 2;
            }
        }
        System.out.println(s.substring(start, end + 1));
        return s.substring(start, end + 1);
    }


    private static int expandAroundCenter(String s, int left, int right) {
        int L = left, R = right;
        //判断左右边界的字符是否相等 只有相等时才返回长度
        while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
            L--;
            R++;
        }
        return R - L - 1;
    }
    /**
     *     public String longestPalindrome(String s) {
     *         List<Character> cs = null;
     *         List<Character> cs1 = null;
     *
     *         String str = "";
     *
     *         //滑动窗口?
     *         for (int l = s.length()-1; l >=0 ; l--) {
     *             //前指针
     *             int b = 0;
     *             //后指针
     *             int c = 0;
     *             for (c =b+l; c <s.length() ; c++,b++) {
     *                 cs = new ArrayList<>();
     *                 cs1 = new ArrayList<>();
     *                 for (int i = b; i <= c; i++) {
     *                     cs.add(s.charAt(i));
     *                 }
     *                 for (int i = 0;i<cs.size();i++){
     *                     cs1.add(cs.get(cs.size()-1-i));
     *                 }
     *                 if (cs.equals(cs1)){
     *                     for (Character character:cs) {
     *                         str+=character;
     *                     }
     *
     *                     System.out.println("最长回文为"+str);
     *                     return str;
     *                 }
     *             }
     *         }
     *         return str;
     *
     *
     *
     */

    //结果一直超时


}
